/*
 * @Author: your name
 * @Date: 2021-06-26 21:26:21
 * @LastEditTime: 2021-06-27 19:05:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 */

import { Color, HorizontalTextAlignment, setDisplayStats, VerticalTextAlignment } from "cc";
import { BaseSystem } from "../frameworks/system/BaseSystem";
import { ConfigSystem } from "../frameworks/system/ConfigSystem";
import { Res } from "../frameworks/res/Res";
import { UISystem } from "../frameworks/system/UISystem";
import QueryVariable from "../frameworks/base/QueryVariable";
import { AutoSizeType, GButton, GGraph, GRoot, GTextField } from "fairygui-cc";
import SystemInfo from "./sdk/SystemInfo";
import { UIRoot } from "./ui/UIRoot";
import { LoadingWindow } from "./ui/window/LoadingWindow";
import { LayerManager } from "../frameworks/base/LayerManager";

export class GameApp {

    private heartInterval!: number | null;

    private systems!: Array<BaseSystem>;

    async start() {
        // dynamicAtlasManager.enabled = false;
        setDisplayStats(false);

        // system
        this.systems = [];
        this.systems.push(new ConfigSystem());
        this.systems.push(new UISystem());
        await this.initSystem();

        // if (GameConfig.Release && QueryVariable.getValue('test') == null)
        //     await this.waitSDKInject();

        // // 玩家数据
        // EntityManager.Add(new PlayerPart());

        LayerManager.inst.init();
        // SceneManager.inst.addScene(SceneType.Login, new LoginScene());
        // SceneManager.inst.addScene(SceneType.Home, new HomeScene());

        if (GameConfig.Debug)
            this.showGmBtn();

        // 预加载
        this.preload();
    }

    private async initSystem() {
        for (let i = 0; i < this.systems.length; ++i) {
            await this.systems[i]["init"]();
        }
    }

    /**
     * 等待SDK注入成功
     * @returns 
     */
    private async waitSDKInject() {
        return new Promise((resolve) => {
            let id = setInterval(() => {
                if (window['gp'] != null) {
                    clearInterval(id);
                    resolve(true);
                }
            }, 10);
        });
    }

    /**
     * 开启心跳
     */
    private startHeartbeat() {
        //心跳/轮询，客户端登录成功后每5秒发一次
        if (this.heartInterval) clearInterval(this.heartInterval);
        this.heartInterval = setInterval(this.heartbeat.bind(this), 5000);
    }

    async heartbeat() {
        // let res: proto.msg.Poll = await Network.poll();
        // if (res?.NoyAry && res?.NoyAry.length > 0) {
        //     for (const notify of res.NoyAry) {
        //         EntityManager.Get(PlayerPart).addRewards(notify.Rewards, true);
        //     }
        // }
    }

    /**
     * 显示GM按钮
     */
    showGmBtn() {

        // let btn = new GButton();
        // btn.setSize(60, 60);

        // let bg = new GGraph();
        // bg.setSize(60, 60);
        // bg.drawRect(1, Color.BLACK, Color.WHITE, [20, 20, 20, 20]);
        // btn.addChild(bg);

        // let txt = new GTextField();
        // txt.autoSize = AutoSizeType.None;
        // txt.setSize(60, 60);
        // txt.text = 'GM';
        // txt.fontSize = 30;
        // txt.color = Color.BLACK;
        // txt.align = HorizontalTextAlignment.CENTER;
        // txt.verticalAlign = VerticalTextAlignment.CENTER;
        // btn.addChild(txt);
        // btn.x = GRoot.inst.width - 70;
        // btn.y = SystemInfo.inst.hasTopMenu ? 70 : 10;
        // btn.draggable = true;
        // btn.onClick(() => {
        //     UIRoot.GetWindow(GmWindow).open();
        // });
        // LayerManager.inst.setLayer(btn, LayerType.UI_Tips);
    }

    async preload() {
        //await Res.loadUIPackageAsync('fgui/loading');
        const winLoading = UIRoot.GetWindow(LoadingWindow);
        // 加载并打开
        await winLoading.open();

        // 加载地图资源
        winLoading.setProgress(50);
        // 加载角色资源
        winLoading.setProgress(100);

        // // 关闭loading界面
        // winLoading.close();
    }
}